<template>
  <div>
    <van-nav-bar
      title="邮寄地址"
      left-text="返回"
      left-arrow
      @click-left="onClickLeft"
    />
    <van-address-list
      v-model="chosenAddressId"
      :list="addresses"
      @add="onAdd"
      @edit="onEdit"
      @select="select"
    />
  </div>
</template>

<script>
import { AddressList, NavBar, Toast } from 'vant'
export default {
  components: {
    [AddressList.name]: AddressList,
    [NavBar.name]: NavBar
  },
  async created () {
    await this.getAddresses()
    if (this.$store.state.address.selectedAddress !== -1) {
      this.chosenAddressId = this.$store.state.address.selectedAddress
    } else {
      const defaultAddress = this.getDefaultAddress()
      if (defaultAddress !== null) {
        this.chosenAddressId = defaultAddress.id
      }
    }
    console.log('chosen:' + this.chosenAddressId)
  },
  data () {
    return {
      chosenAddressId: null,
      addressList: null,
      selectlist: true
    }
  },
  // props () {
  //   selectdefault: {
  //     type: boolean,
  //     default () {return this.selectlist}
  //   }
  // }
  computed: {
    addresses () {
      if (this.addressList === null) {
        return []
      }
      return this.addressList.map(item => { return { id: item.id, name: item.receiver_name, tel: item.receiver_phone, address: item.detail_address, isDefault: item.default_address } })
    }
  },
  methods: {
    async getAddresses () {
      const { data } = await this.$api.account.addressList()
      console.log(data)
      console.log(data.default_address)
      if (data.code !== 200) {
        Toast(data.desc)
      } else {
        this.addressList = data.data
      }
    },
    select (item, index) {
      console.log(index)
      console.log(item)
      if (item.isDefault) {
        console.log('default:' + typeof item.isDefault)
        console.log('default:' + item.isDefault)
        this.selectlist = item.isDefault
        this.chosenAddressId = item.id
        // this.$store.state.address.selectlist = this.selectlist
        // console.log('store:' + this.$store.state.address.selectlist)
      }
      // else {
      //   this.$store.state.address.selectlist = item.isDefault
      // }
      // console.log('store:' + this.$store.state.address.selectlist)
    },
    getDefaultAddress () {
      let defaultAddress = this.addressList ? this.addressList.filter(item => item.default_address === true).shift() : null
      if (defaultAddress === undefined || defaultAddress === null) {
        // return (this.addressList && this.addressList.length > 0) ? this.addressList.first() : null
        // 报错 改掉
        return (this.addresses && this.addresses.length > 0) ? this.addresses[0] : null

      }
      return defaultAddress
    },
    onClickLeft () {
      this.$store.commit('address/setSelectedAddress', this.chosenAddressId)
      this.$router.go(-1)
      // this.$router.push({name: 'payment' ,params: { defaultId: this.selectlist}})
      // eventBus.$on('choiceHospital', function(data) {
      //   console.log('跳转了！！！')
      //
      // }
    },
    onAdd () {
      this.$router.push({ name: 'address-add' })
    },
    onEdit (item, index) {
      this.$router.push({ name: 'address-edit', params: { id: item.id } })
    }
  }
}
</script>

<style>
</style>
